package com.wisemoresystem.seal.service;

import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_file;
import com.wisemoresystem.seal.model.t_bank_job;
import com.wisemoresystem.seal.web.WebTools;

@Service
public class JobService extends BaseService {

	public List<Map<String, Object>> selectJobList() {
		String sql = "select * from t_bank_job t where t.status=0 order by t.code desc";
		List<Map<String, Object>> list = jt.queryForList(sql);
		return list;
	}

	public List<Map<String, Object>> selectLocation() {
		String sql = "select * from t_bank_job t where t.status=0 and t.super_id='root' ";
		List<Map<String, Object>> list = jt.queryForList(sql);
		for (Map<String, Object> map : list) {
			String id = map.get("_id") + "";
			int count = getChildrenCount(id);
			map.put("count", count);
			if (count != 0) {
				map.put("isParent", true);
			} else {
				map.put("isParent", false);
			}
		}
		return list;
	}

	public t_file selectFileById(String file_id) {
		String hql = "from t_file t where t._id=? and t.status=0";
		t_file t = super.findObj(hql, file_id);
		return t;
	}

	@Transactional
	public void saveLocation(List<Map<String, Object>> list) {
		// TODO Auto-generated method stub
		for (Map<String, Object> map : list) {
			String super_code = map.get("super_code") + "";
			t_bank_job t = selectLocationByCode(super_code);
			if (ChkTools.isNotNull(t)) {
				t_bank_job loc = selectLocationByCode(map.get("code") + "");
				if (ChkTools.isNotNull(loc)) {
					loc.setName(map.get("name") + "");
					loc.setSuper_id(t.get_id());
					super.updateObj(loc);
				} else {
					loc = new t_bank_job();
					loc.setCode(map.get("code") + "");
					loc.setName(map.get("name") + "");
					loc.setSuper_id(t.get_id());
					loc.setUser_id(WebTools.getCurrentUserId());
					super.saveObj(loc);
				}
			} else {
				t = new t_bank_job();
				t.setCode(map.get("super_code") + "");
				t.setUser_id(WebTools.getCurrentUserId());
				t.setSuper_id("root");
				super.saveObj(t);
				t_bank_job loc = new t_bank_job();
				loc.setCode(map.get("code") + "");
				loc.setName(map.get("name") + "");
				loc.setUser_id(WebTools.getCurrentUserId());
				loc.setSuper_id(t.get_id());
				super.saveObj(loc);
			}
		}
	}

	public t_bank_job selectLocationByCode(String code) {
		String hql = "from t_bank_job t where t.code=? and t.status=0";
		t_bank_job t = super.findObj(hql, code);
		return t;
	}

	/**
	 * 异步加载地区
	 * 
	 * @return
	 */
	public List<Map<String, Object>> selectLocationByAsync(String location_id) {
		String sql = "select t._id zid,t.* from t_bank_job t where t.status=0 and t.super_id=? order by t.name asc ";
		List<Map<String, Object>> list = jt.queryForList(sql, location_id);
		for (Map<String, Object> map : list) {
			String id = map.get("_id") + "";
			int count = getChildrenCount(id);
			map.put("count", count);
			if (count != 0) {
				map.put("isParent", true);
			} else {
				map.put("isParent", false);
			}
		}
		return list;
	}

	private int getChildrenCount(String id) {
		String sql = "select * from t_bank_job t where t.status=0 and t.super_id=? order by t.name asc ";
		List<Map<String, Object>> list = jt.queryForList(sql, id);
		return list.size();
	}

	/**
	 * 是否存在下级地区
	 * 
	 * @param id
	 * @return
	 */
	public boolean isLive(String id) {
		String sql = "select * from t_bank_job t where t.status=0 and t.super_id=? order by t.name asc ";
		List<Map<String, Object>> list = jt.queryForList(sql, id);
		if (ChkTools.isNotNull(list))
			return true;
		return false;
	}
}
